<?php
//公共函数文件

//统一成功返回
function success($msg = '' , $data = null)
{
    return json([
        'code'=>'1',
        'msg'=>$msg,
        'data'=>$data,
    ]);
}

//统一失败返回
function error($msg = '',$data = null)
{
    return json([
        'code'=>'0',
        'msg'=>$msg,
        'data'=>$data,
    ]);
}

//生成随机字符串
function createRandStr($length = 8)
{
    $str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rendStr = '';
    for ($i = 0;$i < $length;$i++) {
        $rendStr .= $str[mt_rand(0,35)];
    }
    return $rendStr;
}

//生成TOKEN
function signToken($userId, $phone)
{
    $data = [
        //签发人
        'iss' => request()->domain(),
        //签发时间
        'iat' => time(),
        //自定义数据
        'data' => [
            'user_id' => $userId,
            'phone' => $phone,
        ]
    ];
    $key = config('app.jwt_key');
    $token = \Firebase\Jwt\JWT::encode($data, $key, 'HS256');
    return $token;
}